<?php
/**
 * Pertyler DatabaseManagement
 * Control
 * InsertRow
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

if(!isset($_GET['Table']))
	throw new \Exception('Table parameter required.');

require_once __DIR__ . '/../databaseModel/Role.php';

if(!($moderator = Role_Moderator::Select()))
	return require __DIR__ . '/Index.php';

require_once __DIR__ . '/../databaseModel/Table.php';

if(!($table = Table::Select($_GET['Table'])))
	throw new \Exception('Table cannot found.');

if(!$table->SelectPrivilege('insert'))
	throw new \Exception('Table does not have insert privilege.');

if(!Role::Select()->SelectRolePrivilege($table->SelectPrivilege('insert')))
	throw new \Exception('Insert privilege required.');

require_once __DIR__ . '/../library/Pert/General/Page.php';

\Pert\Page::Get()->AddTitle($table);

$Content = \Pert\Page::Get()->Content();

$Content->TableName = (string) $table;

$Content->TableId = $table->Id();

$columnIdArray = array();

$stringColumnArray = array();

$numericColumnArray = array();

$dateColumnArray = array();

$booleanColumnArray = array();

$referancedColumnArray = array();

$referancedColumnTableIdArray = array();

$referancedColumnTableValueArrayArray = array();

foreach($table->SelectColumnArray() AS $column)
{
	$columnIdArray[] = $column->Priority();

	if($column->RelatedTable())
	{
		$referancedColumnArray[$column->Priority()] = (string) $column;

		$referancedColumnTableIdArray[$column->Priority()] = $column->RelatedTable()->Id();

		$referancedColumnTableValueArrayArray[$column->Priority()] = $column->RelatedTable()->SelectPublishedRowArray();
	}
	elseif($column->String())
		$stringColumnArray[$column->Priority()] = (string) $column;
	elseif($column->Numeric())
		$numericColumnArray[$column->Priority()] = (string) $column;
	elseif($column->Date())
		$dateColumnArray[$column->Priority()] = (string) $column;
	elseif($column->Boolean())
		$booleanColumnArray[$column->Priority()] = (string) $column;
}

$Content->ColumnIdArray = $columnIdArray;

unset($columnIdArray);

$Content->StringColumnArray = $stringColumnArray;

unset($stringColumnArray);

$Content->NumericColumnArray = $numericColumnArray;

unset($numericColumnArray);

$Content->DateColumnArray = $dateColumnArray;

unset($dateColumnArray);

$Content->BooleanColumnArray = $booleanColumnArray;

unset($booleanColumnArray);

$Content->ReferancedColumnArray = $referancedColumnArray;

unset($referancedColumnArray);

$Content->ReferancedColumnTableIdArray = $referancedColumnTableIdArray;

unset($referancedColumnTableIdArray);

$Content->ReferancedColumnTableValueArrayArray = $referancedColumnTableValueArrayArray;

unset($referancedColumnTableValueArrayArray);

unset($table);

unset($_GET);

unset($_POST);

echo \Pert\Page::Get()->HeaderHTML();

require __DIR__ . '/../view/InsertRow.php';

echo \Pert\Page::Get()->FooterHTML();

return true;
